It Slices, It Dices...

Actually, it just slices. In this recipe we investigate a new way to extract strings from other strings.
The string you're working with is ''
Enter the beginning index here:
Enter the ending index here:

Discussion

You're already familiar with String.substr(), which extracts a substring from a string using a starting index and a number of characters to take. JavaScript 1.2 introduces String.slice(), which takes a starting index, and an ending index as a way of specifying a substring to return.

The rules for the beginning index and ending index are more complicated than for String.substr(). Given that:

startSlice is the zero-based position at which to begin slicing, and

stopSlice is the zero-based position at which to end slicing,

the following rules apply:

Go back to the top of the page, play around with the form, and see what results this new function gives you.

Copyright ©2000 by Charles River Media, All Rights Reserved